@@ -1,34 +1,34 @@ |
||
1 | 1 |
require 'csv' |
2 | 2 |
|
3 | 3 |
module Agents |
4 |
- class SentimentValueAgent < Agent |
|
5 |
- description <<-MD |
|
6 |
- The SentimentValueAgent generates `good-bad` (psychological valence or happiness index),`active-passive` (arousal), and `strong-weak` (dominance) score. It will output a value between 1 and 9. Make sure the content this agent is running on have sufficient length. |
|
7 |
- Add more stuff |
|
8 |
- MD |
|
4 |
+ class SentimentValueAgent < Agent |
|
5 |
+ description <<-MD |
|
6 |
+ The SentimentValueAgent generates `good-bad` (psychological valence or happiness index),`active-passive` (arousal), and `strong-weak` (dominance) score. It will output a value between 1 and 9. Make sure the content this agent is running on have sufficient length. |
|
7 |
+ Add more stuff |
|
8 |
+ MD |
|
9 | 9 |
|
10 |
- event_description <<-MD |
|
11 |
- Events look like: |
|
12 |
- { |
|
13 |
- :valence => 4.5 |
|
14 |
- :arousal => 4.5 |
|
15 |
- :dominance => 4.5 |
|
16 |
- } |
|
17 |
- MD |
|
10 |
+ event_description <<-MD |
|
11 |
+ Events look like: |
|
12 |
+ { |
|
13 |
+ :valence => 4.5 |
|
14 |
+ :arousal => 4.5 |
|
15 |
+ :dominance => 4.5 |
|
16 |
+ } |
|
17 |
+ MD |
|
18 | 18 |
|
19 |
- default_schedule "every_1h" |
|
19 |
+ default_schedule "every_1h" |
|
20 | 20 |
|
21 |
- def default_options |
|
22 |
- { |
|
21 |
+ def default_options |
|
22 |
+ { |
|
23 | 23 |
|
24 |
- } |
|
25 |
- end |
|
24 |
+ } |
|
25 |
+ end |
|
26 | 26 |
|
27 |
- def working? |
|
28 |
- true |
|
29 |
- end |
|
27 |
+ def working? |
|
28 |
+ true |
|
29 |
+ end |
|
30 | 30 |
|
31 |
- def receive(incoming_events) |
|
31 |
+ def receive(incoming_events) |
|
32 | 32 |
incoming_events.each do |event| |
33 | 33 |
self.memory[:queue] ||= [] |
34 | 34 |
self.memory[:queue] << event.payload |
@@ -39,45 +39,41 @@ module Agents |
||
39 | 39 |
end |
40 | 40 |
|
41 | 41 |
def sentiment_hash |
42 |
- anew = {} |
|
43 |
- CSV.foreach Rails.root.join('app/assets/anew.csv') do |row| |
|
44 |
- anew[row[0]] = [row[2],row[4],row[6]].map {|val| val.to_f} |
|
45 |
- end |
|
46 |
- anew |
|
42 |
+ anew = {} |
|
43 |
+ CSV.foreach Rails.root.join('app/assets/anew.csv') do |row| |
|
44 |
+ anew[row[0]] = [row[2],row[4],row[6]].map {|val| val.to_f} |
|
45 |
+ end |
|
46 |
+ anew |
|
47 | 47 |
end |
48 | 48 |
|
49 | 49 |
def sentiment_values(anew,text) |
50 |
- valence, arousal, dominance, freq = [0] * 4 |
|
51 |
- text.downcase.strip.gsub(/[^a-z ]/,"").split.each do |word| |
|
52 |
- if anew.has_key?(word) |
|
53 |
- valence += anew[word][0] |
|
54 |
- arousal += anew[word][1] |
|
55 |
- dominance += anew[word][2] |
|
56 |
- freq += 1 |
|
57 |
- end |
|
58 |
- end |
|
59 |
- if valence != 0 |
|
60 |
- [valence/freq, arousal/freq, dominance/freq] |
|
61 |
- else |
|
62 |
- ["Insufficient data for meaningful answer"] * 3 |
|
63 |
- end |
|
64 |
- |
|
65 |
- |
|
50 |
+ valence, arousal, dominance, freq = [0] * 4 |
|
51 |
+ text.downcase.strip.gsub(/[^a-z ]/,"").split.each do |word| |
|
52 |
+ if anew.has_key?(word) |
|
53 |
+ valence += anew[word][0] |
|
54 |
+ arousal += anew[word][1] |
|
55 |
+ dominance += anew[word][2] |
|
56 |
+ freq += 1 |
|
57 |
+ end |
|
58 |
+ end |
|
59 |
+ if valence != 0 |
|
60 |
+ [valence/freq, arousal/freq, dominance/freq] |
|
61 |
+ else |
|
62 |
+ ["Insufficient data for meaningful answer"] * 3 |
|
63 |
+ end |
|
66 | 64 |
end |
67 | 65 |
|
68 | 66 |
def check |
69 |
- if self.memory[:queue] && self.memory[:queue].length > 0 |
|
70 |
- anew = sentiment_hash |
|
71 |
- self.memory[:queue].each do |text| |
|
72 |
- if text[:message] |
|
73 |
- sent_values = sentiment_values anew, text[:message] |
|
74 |
- create_event :payload => {:content => text[:message], :valence => sent_values[0], :arousal => sent_values[1], :dominance => sent_values[2]} |
|
75 |
- end |
|
76 |
- end |
|
77 |
- self.memory[:queue] = [] |
|
78 |
- end |
|
67 |
+ if self.memory[:queue] && self.memory[:queue].length > 0 |
|
68 |
+ anew = sentiment_hash |
|
69 |
+ self.memory[:queue].each do |text| |
|
70 |
+ if text[:message] |
|
71 |
+ sent_values = sentiment_values anew, text[:message] |
|
72 |
+ create_event :payload => {:content => text[:message], :valence => sent_values[0], :arousal => sent_values[1], :dominance => sent_values[2]} |
|
73 |
+ end |
|
74 |
+ end |
|
75 |
+ self.memory[:queue] = [] |
|
76 |
+ end |
|
79 | 77 |
end |
80 |
- |
|
81 |
- |
|
82 |
- end |
|
78 |
+ end |
|
83 | 79 |
end |